package com.health.view.spinner;

import android.content.Context;
import android.util.DisplayMetrics;

import com.blankj.utilcode.util.AdaptScreenUtils;
import com.health.tools.SPUtil;

/**
 * ScreenUtils
 * Created by hanj on 14-9-25.
 */
public class ScreenUtils {
    private static int screenW;
    private static int screenH;
    private static float screenDensity;

    public static int getScreenW(Context context){
        if (screenW == 0){
            initScreen(context);
        }
        return screenW;
    }

    public static int getScreenH(Context context){
        if (screenH == 0){
            initScreen(context);
        }
        return screenH;
    }

    public static float getScreenDensity(Context context){
        if (screenDensity == 0){
            initScreen(context);
        }
        return screenDensity;
    }

    private static void initScreen(Context context){
        DisplayMetrics metric = context.getResources().getDisplayMetrics();
        int screenWidth = SPUtil.getInt(context, SPUtil.SP_NAME, "screenWidth");
        int screenHeight = SPUtil.getInt(context, SPUtil.SP_NAME, "screenHeight");
        if (screenWidth > 0 && screenHeight >0) {
//            screenW = screenWidth;
            screenW = AdaptScreenUtils.px2Pt(screenWidth);
            screenH = AdaptScreenUtils.px2Pt(screenHeight);
//            screenH = screenHeight;
        }else {
//            screenW = 760;
//            screenH = 1018;
            screenW = AdaptScreenUtils.px2Pt(800);
            screenH = AdaptScreenUtils.px2Pt(1280);
        }
//        screenW = 800;
//        screenH = 1198;
        screenDensity = metric.density;
    }

    /** 根据手机的分辨率从 dp 的单位 转成为 px(像素) */
    public static int dp2px(Context context, float dpValue) {
        return (int) (dpValue * getScreenDensity(context) + 0.5f);
    }

    /** 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
    public static int px2dp(Context context, float pxValue) {
        return (int) (pxValue / getScreenDensity(context) + 0.5f);
    }
}
